<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将数据写入文件</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.file-get-contents.html">« file_get_contents</a></li>
      <li style="float: right;"><a href="function.file.html">file »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>将数据写入文件</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.file-put-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_put_contents</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_put_contents</span> &mdash; <span class="dc-title">将数据写入文件</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.file-put-contents-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_put_contents</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$data</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment"> 
   和依次调用 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>，<span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span>
   以及 <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span> 功能一样。
  </p>
  <p class="para">
   如果 <code class="parameter">filename</code>
   不存在，将会创建文件。反之，存在的文件将会重写，除非设置
   <strong><code>FILE_APPEND</code></strong> flag。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       要被写入数据的文件名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">data</code></dt>

     <dd>

      <p class="para">
       要写入的数据。类型可以是 <span class="type">string</span>，<span class="type">array</span>
       或者是 <span class="type"><span class="type stream">stream</span></span> 资源（如上面所说的那样）。
      </p>
      <p class="para">
       如果 <code class="parameter">data</code>
       指定为 stream 资源，这里 stream
       中所保存的缓存数据将被写入到指定文件中，这种用法就相似于使用
       <span class="function"><a href="function.stream-copy-to-stream.html" class="function">stream_copy_to_stream()</a></span> 函数。
      </p>
      <p class="para">
       参数 <code class="parameter">data</code> 可以是数组（但不能为多维数组），这就相当于
       <code class="literal">file_put_contents($filename, join(&#039;&#039;, $array))</code>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags</code> 的值可以是
       以下 flag 使用 OR (<code class="literal">|</code>) 运算符进行的组合。
      </p>
       <p class="para">
       <table class="doctable table">
        <caption><strong>Available flags</strong></caption>
        
         <thead>
          <tr>
           <th>Flag</th>
           <th>描述</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>
            <strong><code>FILE_USE_INCLUDE_PATH</code></strong>
           </td>
           <td>
            在 include 目录里搜索  <code class="parameter">filename</code>。
            更多信息可参见 <a href="ini.core.html#ini.include-path" class="link">include_path</a>。
           </td>
          </tr>

          <tr>
           <td>
            <strong><code>FILE_APPEND</code></strong>
           </td>
           <td>
            如果文件 <code class="parameter">filename</code>  已经存在，追加数据而不是覆盖。
           </td>
          </tr>

          <tr>
           <td>
            <strong><code>LOCK_EX</code></strong>
           </td>
           <td>
            在写入时获取文件独占锁。换句话说，在调用 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>
            和 <span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span> 中间发生了 <span class="function"><a href="function.flock.html" class="function">flock()</a></span>
            调用。这与调用带模式“x”的 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 不同。
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       一个 context 资源。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-put-contents-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   该函数将返回写入到文件内数据的字节数，失败时返回<strong><code>false</code></strong>
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-put-contents-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2160">
    <p><strong>示例 #1 简单用法示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;打开文件获取已经存在的内容<br /></span><span style="color: #0000BB">$current&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;追加新成员到文件<br /></span><span style="color: #0000BB">$current&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"John&nbsp;Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;将内容写回文件<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$current</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-2161">
    <p><strong>示例 #2 Using flags</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;new&nbsp;person&nbsp;to&nbsp;add&nbsp;to&nbsp;the&nbsp;file<br /></span><span style="color: #0000BB">$person&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"John&nbsp;Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;Write&nbsp;the&nbsp;contents&nbsp;to&nbsp;the&nbsp;file,&nbsp;<br />//&nbsp;using&nbsp;the&nbsp;FILE_APPEND&nbsp;flag&nbsp;to&nbsp;append&nbsp;the&nbsp;content&nbsp;to&nbsp;the&nbsp;end&nbsp;of&nbsp;the&nbsp;file<br />//&nbsp;and&nbsp;the&nbsp;LOCK_EX&nbsp;flag&nbsp;to&nbsp;prevent&nbsp;anyone&nbsp;else&nbsp;writing&nbsp;to&nbsp;the&nbsp;file&nbsp;at&nbsp;the&nbsp;same&nbsp;time<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$person</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILE_APPEND&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.file-put-contents-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
  <div class="tip"><strong class="tip">小技巧</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
    <li class="member"><span class="function"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - 创建资源流上下文</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>